package org.leetcode.middle.leetcode73;

import java.util.Arrays;

public class Solution {

    public void setZeroes5(int[][] matrix){
        int n1=matrix.length;
        int n2=matrix[0].length;
        boolean [][] helper = new boolean[n1][n2];

        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                if (matrix[i][j]==0){
                    Arrays.fill(helper[i],true);
                    for (int k = 0; k < n1; k++) {
                        helper[k][j]=true;
                    }
                }
            }
        }

        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                if (helper[i][j]){
                    matrix[i][j]=0;
                }
            }
        }
    }

    public void setZeroes3(int[][] matrix)
    {
        int m = matrix.length;
        int n = matrix[0].length;

        boolean [] row = new boolean[m];
        boolean [] col = new boolean[n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if ( matrix[i][j]==0)
                {
                    row[i]=true;
                    col[j]=true;
                }
            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (row[i] || col[j] ){
                    matrix[i][j]=0;
                }
            }
        }

    }


    public void setZeroes2(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        boolean[] row = new boolean[m];
        boolean[] col = new boolean[n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 0) {
                    row[i] = col[j] = true;
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (row[i] || col[j]) {
                    matrix[i][j] = 0;
                }
            }
        }
    }




    public void setZeroes(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j]==0)
                {
                    Arrays.fill(matrix[i],0);
                }
            }
        }
    }

    public static void main(String[] args) {
        int [] [] test = {{0,1,2,0},{3,4,5,2},{1,3,1,5}};

        Solution solution = new Solution();
        solution.setZeroes2(test);
    }
}
